<?php
	class Db_banner extends CI_Model{
		public function __construct(){
			parent::__construct();
		}
		
		public function banner_all(){
			$aColumns = array( 't1.pkid','t1.banner','t2.name','t1.url as url1','t1.pkid','t1.pkid');
			/* กำหนด primary key ให้กับคอลัมน์ */
			$sIndexColumn = "t1.pkid";
			/* ชื่อตาราง */
			$sTable = "mobile_banner t1 JOIN mobile_customer t2 ON t1.customer_pkid=t2.pkid";
			/*
			 * แบ่งหน้า
			*/
			$sLimit = "";
			if ($this->input->post('iDisplayStart') != ""  && ($this->input->post('iDisplayLength') != '-1' ))
			{
				$sLimit = "LIMIT ".$this->input->post('iDisplayStart') .", ".$this->input->post('iDisplayLength') ;
			}	
			/*
			 * จัดเรียงลำดับ
			*/
			$sOrder = "";
			if ($this->input->post('iSortCol_0') != "" )
			{
				$sOrder = "ORDER BY  ";
				for ( $i=0 ; $i<intval( $this->input->post('iSortingCols')) ; $i++ )
				{
					if ($this->input->post('bSortable_'.intval($this->input->post('iSortCol_'.$i)) ) == "true" )
					{
						$sOrder .= $aColumns[ intval( $this->input->post('iSortCol_'.$i) ) ]."
						".$this->input->post('sSortDir_'.$i) .", ";
					}
				}
					
				$sOrder = substr_replace( $sOrder, "", -2 );
				if ( $sOrder == "ORDER BY" )
				{
					$sOrder = "";
				}
			}
		
		
			$sWhere = "";
			if ($this->input->post('sSearch') != "" && $this->input->post('sSearch') != "" )
			{
				$sWhere = "WHERE (";
				for ( $i=0 ; $i<count($aColumns) ; $i++ )
				{
					$sWhere .= $aColumns[$i]." LIKE '%". $this->input->post('sSearch') ."%' OR ";
				}
				$sWhere = substr_replace( $sWhere, "", -3 );
				$sWhere .= ')';
			}
		
			/* เวลาค้นหาในคอลัมน์  */
			for ( $i=0 ; $i<count($aColumns) ; $i++ )
			{
				if ( $this->input->post('bSearchable_'.$i) != "" && $this->input->post('bSearchable_'.$i) == "true" && $this->input->post('sSearch_'.$i) != '' )
				{
					if ( $sWhere == "" )
					{
						$sWhere = "WHERE ";
					}
					else
					{
						$sWhere .= " AND ";
					}
					$sWhere .= $aColumns[$i]." LIKE '%".$this->input->post('sSearch_'.$i)."%' ";
				}
			}
		
		
			/*
			 *คำสั่ง SQL ดึงข้อมูลออกมาโชว์
			*
			*/
			$sQuery = "
			SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns))."
			FROM   $sTable
			$sWhere
			$sOrder
			$sLimit
			";
			$rResult = $this->db->query( $sQuery);
		
			/* จำนวนข้อมูลหลังจากค้นหา */
			 $sQuery = "
			 SELECT FOUND_ROWS() as numT
			 ";
			 $rResultFilterTotal = $this->db->query($sQuery) ;
			 $aResultFilterTotal = $rResultFilterTotal->row();
			 $iFilteredTotal = $aResultFilterTotal->numT;
		
			 /*  จำนวนทั้งหมด */
			$sQuery = "
			SELECT COUNT(".$sIndexColumn.") as num
					FROM   $sTable
					";
					$rResultTotal = $this->db->query( $sQuery);
					$aResultTotal = $rResultTotal->row();
					$iTotal = $aResultTotal->num;
		
		
					/*
					* ส่วนการแสดงผล
			*/
			$output = array(
			 "sEcho" => intval($this->input->post('sEcho')),
			 		"iTotalRecords" => $iTotal,
			 		"iTotalDisplayRecords" => $iFilteredTotal,
			 		"aaData" => array()
			 );
		
		
		
			 		foreach ($rResult->result_array()  as $aRow)  //loop ออกมาแสดงข้อมูล
			 		{
			 			$row = array();
			 			for ( $i=0 ; $i<count($aColumns) ; $i++ )
			 			{
			 			if ( $aColumns[$i] == "version" )
			 			{
			 				$row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
			 					
			 				}
			 				else if ( $aColumns[$i] != ' ' )
			 				{
			 				if($i == 0){
			 				$row[] = '<input type="checkbox" name="check[]" value="'.$aRow['pkid'].'" />';
			 				}else if($i == 1){
			 					$row[] = $aRow['name'];
							}else if($i == 2){
			 					$row[] = '<img src="'.base_url().'media/banner/'.$aRow['banner'].'">';
			 				}else if($i == 3){
			 					$row[] = $aRow['url1'];
			 				}
			 				else if($i == 4){
				 				$row[] = '
				 				<a href="'.base_url().'manage_banner/banner_edit/'.$aRow['pkid'].'" >
				 				<img border="0" src="'.base_url().'images/edit-icon.png">
				 				</a>';
			 				}
							else if($i == 5){
				 				$row[] = '
				 				<a href="'.base_url().'manage_banner/banner_del/'.$aRow['pkid'].'" >
				 				<img border="0" src="'.base_url().'images/delete-icon.png">
				 				</a>';
			 				}
			 					
			 				}
			 			}
			 			$output['aaData'][] = $row;
			 			}
			 			return $output;
			 			//	echo json_encode( $output );
			 		}
			
		
		public function customer($id=""){
			$this->db->order_by("pkid",'asc');
			$res = $this->db->get('mobile_customer');
			$str ='<option value="">=select customer=</option>';
			foreach ($res->result_array() as $P){
				if($id == $P['pkid']) $check = 'selected="selected"';
				else $check ="";
				$str .='<option value="'.$P['pkid'].'" '.$check.'>'.$P['name'].'</option>';
			}
			return $str;
		}
		
		public function banner_type($L =""){
			$L1="";$L2="";$L3="";$L4="";
			if($L == 1){
				$L1 = 'selected="selected"';
			}else if($L == 2){
				$L2 = 'selected="selected"';
			}else if($L == 3){
				$L3 = 'selected="selected"';
			}else if($L == 4){
				$L4 = 'selected="selected"';
			}
			$str =  '<option value="">=select type banner=</option>"';
			$str .= '<option value="1" '.$L1.'>Top</option>"';
			$str .= '<option value="2" '.$L2.'>Bottom</option>"';
			$str .= '<option value="3" '.$L3.'>Left</option>"';
			$str .= '<option value="4" '.$L4.'>Right</option>"';
			return $str;
		}
		
		public function  stripdate($str){
			list($y,$m,$d) = explode("-", $str);
			$d = substr($d, 0,2);
			$str = $y.'-'.$m.'-'.$d;
			return $str;
		}
		

		public function format_date($mysql_timestamp, $type) {
		
			$timestamp = strtotime($mysql_timestamp); // convert to timestamp
			
			if ($type == "DT") {
			$format_date = date("d M Y", $timestamp);
			} elseif ($type == "D") {
			$format_date = date("d/m/Y", $timestamp);
			} elseif ($type == "T") {
			$format_date = date("H:i:s", $timestamp);
			} elseif ($type == "DAY") {
			$format_date = date("d", $timestamp);
			} elseif ($type == "DAYDATE") {
			$format_date = date("M", $timestamp);
			}
		
		return $format_date;
		
		} 
		
		
		public function ads_all(){
			$config['base_url'] = base_url().'manage_banner/ads_list';
			$config['total_rows'] = $this->db->count_all("hotel_ads");
			$config['per_page'] = 20;
			$config['full_tag_open'] = '<div class="pages">';
			$config['full_tag_close'] = '<div>';
			$this->pagination->initialize($config);
			$this->db->select('t1.name as pname,t2.pkid,t2.ads,t2.img');
			$this->db->from("hotel_partner t1");
			$this->db->join("hotel_ads t2","t1.pkid=t2.partner_pkid");
			$this->db->limit($config['per_page'],$this->uri->segment(3));
			$this->db->order_by("t2.pkid", "desc");
			$res = $this->db->get();
			//==
			$no = $this->uri->segment(3)+1;
			$i = 1;
			$arr = $res->result_array();
			$cat = array();
			foreach ($arr as $P){
				if($i %2 == 0)
					$tmp['back']							="#FFFFFF";
				else
					$tmp['back']							="#F4F4F4";
				$i++;
				$tmp['no']							=$no;
				$tmp['pkid']						=$P['pkid'];
				$tmp['pname']					=$P['pname'];
				$tmp['ads']						=$P['ads'];
				if(trim($P['img']) != "")
					$tmp['img']					='<img src="'.base_url().'media/ads/'.$P['img'].'">';
				$cat[] = $tmp;
				$no = $no+1;
			}
			$num_row = $config['total_rows'];
			$pagelink  = $this->pagination->create_links();
			return array($cat,$num_row,$pagelink,$config['total_rows']);
		}
	}
?>